layui.use(['table','form','laydate','jquery'], function(){
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var $ = layui.$; //重点处
    //第一个实例
    table.render({
        elem: '#table_list',
        limit: 30,
        height: 'full-330',
        method: 'post',
        text: {none: '暂无相关数据'},
        url: '/logic/logic_shares/userEmail', //数据接口
        page: true, //开启分页
        toolbar:"#table_list_tool",
        cols: [[ //表头
            {checkbox: true},
            {field: 'name', title: '姓名', width:120,align:"center",},
            {field: 'email', title: '邮箱地址', width:220,align:"center",}
        ]],
        parseData: function(result){
            return CommonUtil.layuiParseData(result);
        },
        where:{id:$("#id").val()}
    });

    //搜索初始化
    form.on('submit(search)', function(data){
        //阻止表单跳转。如果需要表单跳转，去掉这段即可。 默认返回 false
        return CommonUtil.layuiInitSearch("table_list",data);
    });

    //工具栏监听事件
    table.on('toolbar(table_list_filter)', function(obj){
        switch(obj.event){
            case 'add': CommonUtil.layuiEditPage("/logic/logic_shares/addEmailUser?id="+$("#id").val(),"新增",580,600,obj); break;
            case 'del': removeRows(obj); break;
        };
    });
    function removeRows(obj){
        var old = table.cache[obj.config.id];
        var check = table.checkStatus(obj.config.id).data;

        const result =[];
        for (var i = 0; i < old.length; i++) {
            var flag = true;
            //当删除数据id 等于 原有数据的时候 不删除
            for (var j = 0; j < check.length; j++) {
                if(old[i].id==check[j].id){
                    flag =false;
                    break;
                }
            }
            if(flag){
                result.push(old[i]);
            }
        }
        layui.table.reload('table_list', {
            url:'',
            data:result
        });
    }

});
function addUserEmail(emails){
    let addData = JSON.parse(JSON.stringify(emails));
    let data = layui.table.cache['table_list'];
    let result = [];
    for (let i = 0; i < addData.length; i++) {
        let flag = true;
        for (let j = 0; j < data.length; j++) {
            if(addData[i].id==data[j].id){
                flag = false;
                break;
            }
        }
        //当新增数据在原有数据中不存在 才能新增数据
        if (flag){
            result.push(addData[i])
        }
    }
    result = data.concat(result);
    layui.table.reload('table_list', {
        url:'',
        data:result
    });
}
